"""
爬取斗鱼英雄联盟的信息，每条数据包括标题，类别，房间主人的名字，观看数量，图片地址(先不做)
"""
import time
from selenium import webdriver
from tqdm import tqdm


class DouYu:
    def __init__(self):
        self.url = 'https://www.douyu.com/g_LOL'
        self.driver = webdriver.Chrome(executable_path=r'E:\ChromeDriver\chromedriver_win32/chromedriver.exe')
        self.driver.maximize_window()
        pass

    def run(self):
        # 获取url
        # 创建浏览器对象
        # 发送请求
        self.driver.get(self.url)
        # self.driver.implicitly_wait(10)
        js = 'scrollTo(0, {})'.format(1000)
        self.driver.execute_script(js)

        temp = []
        while True:
            time.sleep(3)  # 手动设置等待时间，防止网络过慢数据还没加载出来导致爬取数据无效

            # 获取数据
            el_title_list = self.driver.find_elements_by_xpath('//*[@id="listAll"]/div[2]/ul/li/div/a/div[2]/div[1]/h3')
            el_type_list = self.driver.find_elements_by_xpath('//*[@id="listAll"]/div[2]/ul/li/div/a/div[2]/div[1]/span')
            el_owner_list = self.driver.find_elements_by_xpath('//*[@id="listAll"]/div[2]/ul/li/div/a/div[2]/div[2]/h2')
            el_num_list = self.driver.find_elements_by_xpath('//*[@id="listAll"]/div[2]/ul/li/div/a/div[2]/div[2]/span')
            el_image_list = self.driver.find_elements_by_xpath('//*[@id="listAll"]/div[2]/ul/li/div/a/div[1]/div[1]/img')

            # print(len(el_title_list))
            # raise Exception('ting')
            # print(el_title_list, el_type_list)
            # print(el_num_list)
            # 解析数据

            for el_title, el_type, el_owner, el_num, el_image in tqdm(zip(el_title_list, el_type_list, el_owner_list, el_num_list,
                                                                    el_image_list), total=len(el_type_list)):
                # for scroll_index in range(18):
                #     js = 'scrollTo(0, {})'.format(scroll_index * 500 + 500)
                #     self.driver.execute_script(js)
                #     time.sleep(1)
                # try:
                cur_title = el_title.text
                cur_type = el_type.text
                cur_owner = el_owner.text
                cur_num = el_num.text
                cur_image = el_image.get_attribute('src')
                temp.append({'title': cur_title, 'type': cur_type, 'owner': cur_owner, 'num': cur_num, 'image': cur_image})
                # except:
                #     continue

            # # 下一页
            # next_button = self.driver.find_element_by_xpath('//*[@id="listAll"]/div[2]/div/ul/li[9]')
            try:
                next_button = self.driver.find_element_by_xpath('//*[@id="listAll"]/div[2]/div/ul/li[@class=" dy-Pagination-next"]')
                self.driver.execute_script('scrollTo(0, 10000)')
                next_button.click()
            except:
                break

        print(temp)
        print(len(temp))

        # self.driver.quit()


if __name__ == '__main__':
    douyu = DouYu()
    douyu.run()


# 下一页class
# dy-Pagination-item-custom xh-highlight
# dy-Pagination-item-custom xh-highlight

# dy-Pagination-next
# dy-Pagination-disabled dy-Pagination-next
